package com.mall.user.services;

import com.alibaba.fastjson.JSON;
import com.mall.user.ILoginService;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.converter.MemberConverter;
import com.mall.user.dal.entitys.Member;
import com.mall.user.dal.persistence.MemberMapper;
import com.mall.user.dto.CheckAuthRequest;
import com.mall.user.dto.CheckAuthResponse;
import com.mall.user.dto.UserLoginRequest;
import com.mall.user.dto.UserLoginResponse;
import com.mall.user.utils.JwtTokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.DigestUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @Author: DLX
 * @Date: 2021/6/20 18:08
 * @Description:
 **/
@Service
@Slf4j
public class LoginServiceImpl implements ILoginService {

    @Autowired
    MemberMapper memberMapper;

    @Autowired
    MemberConverter memberConverter;

    @Override
    public UserLoginResponse login(UserLoginRequest loginRequest) {
        UserLoginResponse response = new UserLoginResponse();
        // 参数校验
        loginRequest.requestCheck();
        // 登录验证
        String userName = loginRequest.getUserName();
        String userPwd = DigestUtils.md5DigestAsHex(loginRequest.getPassword().getBytes());

        Example example = new Example(Member.class);
        example.createCriteria().andEqualTo("username", userName).andEqualTo("password", userPwd);
        List<Member> memberList = memberMapper.selectByExample(example);
        // 用户名或密码错误
        if (CollectionUtils.isEmpty(memberList)) {
            response.setCode(SysRetCodeConstants.DATA_NOT_EXIST.getCode());
            response.setMsg(SysRetCodeConstants.DATA_NOT_EXIST.getMessage());
            return response;
        }
        // 未激活
        Member member = memberList.get(0);
        if ("N".equals(member.getIsVerified())) {
            response.setCode(SysRetCodeConstants.USER_ISVERFIED_ERROR.getCode());
            response.setMsg(SysRetCodeConstants.USER_ISVERFIED_ERROR.getMessage());
            return response;
        }
        // 登录成功
        UserLoginResponse loginResponse = memberConverter.member2UserLoginResponse(member);
        log.info("登录成功,参数request:{}", JSON.toJSONString(loginRequest));
        // token
        String data = loginResponse.getUsername();// token根据什么生成
        JwtTokenUtils tokenUtils = JwtTokenUtils.builder().msg(data).build();
        String token = tokenUtils.creatJwtToken();
        loginResponse.setToken(token);
        loginResponse.setCode(SysRetCodeConstants.SUCCESS.getCode());
        loginResponse.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
        return loginResponse;
    }

    @Override
    public CheckAuthResponse validToken(CheckAuthRequest checkAuthRequest) {
        CheckAuthResponse checkAuthResponse = new CheckAuthResponse();
        String token = checkAuthRequest.getToken();
        JwtTokenUtils jwtTokenUtils = JwtTokenUtils.builder().token(token).build();
        String s = jwtTokenUtils.freeJwt();
        checkAuthResponse.setUserinfo(s);
        return checkAuthResponse;
    }
}
